import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.utils import ForumDriver

class PersonalCenter:
    def __init__(self):
        self.driver=ForumDriver.driver
        # 点击右上角的登录用户图片，出现3个选项
        self.driver.find_element(By.XPATH, '/html/body/div[1]/header[1]/div/div/div[3]').click()
        # 点击“个人中心”
        self.driver.find_element(By.XPATH, '//*[@id="index_user_settings"]').click()

    def _clear_input(self):
        # 清空3个输入框:原密码，新密码，确认密码
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_oldPassword"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_newPassword"]').clear()
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_passwordRepeat"]').clear()

    def PersonalCenterTest(self):
        # 1.测试是否存在关键元素：用户中心、我的账户、头像、昵称
        elements = ['//*[@id="bit-forum-content"]/div[1]/div/div/div/h2',
                    '//*[@id="bit-forum-content"]/div[2]/div/div/div/div[1]/div/div/a',
                    '//*[@id="settings_avatar"]',
                    '//*[@id="settings_nickname"]']
        for e in elements:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 2.修改昵称(修改邮箱地址、电话号码和个人简介都是相同的方法)
        self.driver.find_element(By.XPATH,'//*[@id="setting_input_nickname"]').clear()
        new_nickname=str(int(time.time()))
        self.driver.find_element(By.XPATH,'//*[@id="setting_input_nickname"]').send_keys(new_nickname)
        self.driver.find_element(By.XPATH,'//*[@id="setting_submit_nickname"]').click()
        # nickname = self.driver.execute_script('return $("#setting_input_nickname").val()')
        nickname=self.driver.find_element(By.XPATH,'//*[@id="setting_input_nickname"]').get_attribute("value")
        time.sleep(2)
        assert nickname==new_nickname
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 3.修改密码
        # 滚动到底部
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        # 3.1修改失败，还是在本页面
        test_cases=[
            # 描述，原密码，新密码，确认密码
            ("都为空","","",""),
            ("空原密码","","12","12"),
            ("空新密码","123456","","12"),
            ("空确认密码","123456","12",""),
            ("原密码错误","1234","12","12"),
            ("新密码和确认密码不同","123456","12","2"),
        ]
        for desc,old_pwd,new_pwd,confirm_pwd in test_cases:
            self._clear_input()
            # 输入数据
            if old_pwd:
                self.driver.find_element(By.XPATH,'//*[@id="settings_input_oldPassword"]').send_keys(old_pwd)
            if new_pwd:
                self.driver.find_element(By.XPATH,'//*[@id="settings_input_newPassword"]').send_keys(new_pwd)
            if confirm_pwd:
                self.driver.find_element(By.XPATH,'//*[@id="settings_input_passwordRepeat"]').send_keys(confirm_pwd)

            time.sleep(2)
            # 点击“提交修改”
            self.driver.find_element(By.XPATH,'//*[@id="settings_submit_password"]').click()
            # 测试是否还在本页面，通过查找元素“个人简介”
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.XPATH, '//*[@id="bit-forum-content"]/div[2]/div/div/div/div[2]/div/h3[5]'))
            )
            # 截图
            ForumDriver.getScreenShot()
            time.sleep(1)

        # 3.2修改成功，跳转到登录界面
        self._clear_input()
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_oldPassword"]').send_keys("123456")
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_newPassword"]').send_keys("12")
        self.driver.find_element(By.XPATH, '//*[@id="settings_input_passwordRepeat"]').send_keys("12")
        time.sleep(2)
        # 点击“提交修改”
        self.driver.find_element(By.XPATH, '//*[@id="settings_submit_password"]').click()
        # 测试是否跳转到了登陆页面
        time.sleep(1)
        curUrl=self.driver.current_url
        assert curUrl=="http://127.0.0.1:58080/sign-in.html"
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)



